package com.app.controller;

import cn.hutool.core.util.StrUtil;
import com.app.common.ApplyTool;
import com.app.mapper.ApplyMapper;
import com.app.mapper.PersonMapper;
import com.app.model.ApplyModel;
import com.app.model.PersonModel;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.vo.LevelVo;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;

@RestController
@Controller
@RequestMapping("/api")
public class ApplyController {
    @Autowired
    private ApplyMapper applyMapper;
    @Autowired
    private PersonMapper personMapper;


    @GetMapping("/applyType")
    public Result<Object> applyType() {
        return Result.success(ApplyTool.LEVEL_VO_LIST);
    }

    //新增和编辑
    @PostMapping("/addApply")
    public Result<Object> addApply(@RequestBody ApplyModel applyModel) {
        if (applyModel.getId() != null && applyModel.getId() > 0) {
            applyModel.setUpdateTime(CommonHelp.getNowTimestamp());
            applyMapper.updateById(applyModel);
            return Result.success("更新成功");
        } else {
            applyModel.setCreateTime(CommonHelp.getNowTimestamp());
            applyModel.setCancellation(1);
            applyMapper.insert(applyModel);
            return Result.success("新增成功");
        }
    }

    //删除
    @GetMapping("/deleteApplyById")
    public Result<Object> deleteApplyById(Integer id) {
        applyMapper.deleteById(id);
        return Result.success("删除成功");
    }

    @GetMapping("/detailById")
    public Result<ApplyModel> detailById(Integer id) {
        ApplyModel applyModel = applyMapper.selectById(id);
        HashMap<Integer, String> integerStringHashMap = new HashMap<>();
        for (LevelVo levelVo : ApplyTool.LEVEL_VO_LIST) {
            integerStringHashMap.put(levelVo.getId(), levelVo.getLevelName());
        }
        applyModel.setTypeText(integerStringHashMap.get(applyModel.getType()));
        PersonModel personModel = personMapper.selectById(applyModel.getPersonId());
        applyModel.setName(personModel.getName());
        return Result.success(applyModel);
    }


    /**
     * 请假的列表查询
     */
    @GetMapping("/applyList")
    public Result<List<ApplyModel>> list(@RequestParam(required = false) String startTime,
                                         @RequestParam(required = false) String endTime) {
        if (StrUtil.isBlank(startTime) || StrUtil.isBlank(endTime)) {
            // 获取当前日期和时间
            LocalDateTime currentDateTime = LocalDateTime.now();
            // 获取当前日期的起始时间和结束时间
            startTime = Timestamp.valueOf(currentDateTime.toLocalDate().atStartOfDay()).toString();
            endTime = Timestamp.valueOf(currentDateTime.toLocalDate().atTime(LocalTime.MAX)).toString();
        }
        List<PersonModel> personModels = personMapper.selectList(null);
        HashMap<Integer, PersonModel> integerPersonModelHashMap = new HashMap<>();
        for (PersonModel personModel : personModels) {
            integerPersonModelHashMap.put(personModel.getId(), personModel);
        }
        List<LevelVo> levelVoList = ApplyTool.LEVEL_VO_LIST;
        HashMap<Integer, String> integerStringHashMap = new HashMap<>();
        for (LevelVo levelVo : levelVoList) {
            integerStringHashMap.put(levelVo.getId(), levelVo.getLevelName());
        }
        Wrapper<ApplyModel> wq = new QueryWrapper<ApplyModel>()
                .apply("NOT(end_time <' " + startTime + " 'OR start_time > '" + endTime + "')");
        List<ApplyModel> applyModels = applyMapper.selectList(wq);
        applyModels.forEach(applyModel -> {
            applyModel.setTypeText(integerStringHashMap.get(applyModel.getType()));
            applyModel.setName(integerPersonModelHashMap.get(applyModel.getPersonId()).getName());
        });
        return Result.success(applyModels);
    }

    /**
     * 销假   lm_apply表的id
     *
     * @return
     */
    @GetMapping("/cancellationOfLeave")
    public Result cancellationOfLeave(Integer id, Integer cancellation, String remark) {
        if (id == null || id <= 0) {
            return Result.error(400, "参数错误，id不能为空");
        }
        ApplyModel applyModel = applyMapper.selectById(id);
        if (applyModel == null) {
            return Result.error(400, "请假数据不存在，无法销假");
        }
        applyModel.setUpdateTime(CommonHelp.getNowTimestamp());
        applyModel.setCancellation(cancellation);
        applyModel.setRemark(remark);
        applyMapper.updateById(applyModel);
        return Result.success("销假成功");
    }
}
